<template>
  <div class="analysis" :class="{ padbottom: !isCheck }">
    <div>
      <van-checkbox-group v-model="result">
        <div
          v-for="(item, index) in analysisData"
          :key="item.involve_system_name"
          class="module"
        >
          <van-cell-group>
            <van-cell v-if="currentNode == 'T10347' || currentNode == 'T10091'" class="choose">
              <van-checkbox
                shape="square"
                icon-size="14px"
                checked-color="#009966"
                :name="item.id"
              ></van-checkbox>
            </van-cell>
            <van-field
              v-model="item.involve_system_name"
              label="系统名称"
              input-align="right"
              label-class="labelColor"
              placeholder="系统名称只读"
              readonly
            />
            <van-cell center title="业务确认">
              <template #right-icon>
                <van-switch
                  v-model="item.is_confirm"
                  size="24"
                  @change="saveData"
                  :disabled="!isAnalysisNode"
                />
              </template>
            </van-cell>
            <van-field
              v-model="item.demand_specification"
              label-width="7em"
              label="需求规则说明书"
              input-align="right"
              label-class="labelColor"
              placeholder="说明书"
              :readonly="!isAnalysisNode"
            />
            <!-- <van-field
              v-if="isAnalysisNode"
              v-model="item.advice_confirm_time"
              is-link
              label-class="labelColor"
              readonly
              label-width="10em"
              arrow-direction="down"
              label="建议用户确认反馈时间"
              @click="getDate(index, 1)"
              input-align="right"
              placeholder="请选择"
            />
            <van-field
              v-else
              v-model="item.advice_confirm_time"
              label="建议用户确认反馈时间"
              label-width="10em"
              input-align="right"
              label-class="labelColor"
              placeholder="建议用户确认反馈时间"
              readonly
            /> -->
            <van-field
              v-model="item.advice_confirm_time"
              label="建议用户确认反馈时间"
              label-width="10em"
              input-align="right"
              label-class="labelColor"
              placeholder="建议用户确认反馈时间"
              readonly
            />
            <van-field
              v-if="isAnalysisNode"
              v-model="item.expect_acceptance_time"
              is-link
              label-class="labelColor"
              readonly
              label-width="10em"
              arrow-direction="down"
              label="建议提交用户验收时间"
              @click="getDate(index, 2)"
              input-align="right"
              placeholder="请选择"
            />
            <van-field
              v-else
              v-model="item.expect_acceptance_time"
              label="建议提交用户验收时间"
              label-width="10em"
              input-align="right"
              label-class="labelColor"
              placeholder="建议提交用户验收时间"
              readonly
            />
          </van-cell-group>
        </div>
      </van-checkbox-group>
    </div>
    <van-calendar
      v-model="show"
      :show-confirm="false"
      color="#009966"
      @confirm="onConfirm"
    />
    <div v-if="isCurrentNode" class="form-btn">
      <div class="btn btn-color1" @click="handleMoreOpera">更多操作</div>
      <!-- <div class="btn btn-color2">保存</div> -->
      <div class="btn btn-color3" @click="submit">提交</div>
    </div>
    <van-action-sheet
      v-model="showMoreOpera"
      :actions="actions"
      cancel-text="取消"
      close-on-click-action
      @select="onSelect"
      @cancel="onCancel"
    />
  </div>
</template>
<script>
import { serviceAnalysis, serviceAnalysisSubmit } from "@/api/service.js";
import { Toast } from "vant";
export default {
  props: ["id", "currentNode","baseData"], //服务单id
  data() {
    return {
      analysisData: [
        {
          involve_system_name: "",
          is_confirm: false,
        },
      ],
      text: "13123123",
      date: "",
      show: false,
      isswitch: false,
      actions: [{ name: "OA流程" }, { name: "审批记录" }],
      showMoreOpera: false,
      isCheck: false,
      index: 0,
      dateType: 1,
      isCurrentNode: true, //是否在当前节点下操作
      isAnalysisNode: true, // 是否是需求分析节点
      result: [],
    };
  },
  mounted() {
    if (this.id) {
      this.getAnalysisData();
      this.checkNode();
    }
  },
  methods: {
    handleMoreOpera() {
      this.showMoreOpera = true;
    },
    onCancel() {
      this.showMoreOpera = false;
    },
    //检查是否是当前节点
    checkNode() {
      console.log(this.currentNode);
      if (
        this.currentNode == "T10088" ||
        this.currentNode == "T10347" ||
        this.currentNode == "T10091" ||
        this.currentNode == "T10269"
      ) {
        this.isCurrentNode = true;
      } else {
        this.isCurrentNode = false;
      }
      if (this.currentNode == "T10088") {
        this.isAnalysisNode = true;
      } else {
        this.isAnalysisNode = false;
      }
    },
    submit() {
      // Toast("提交");
      console.log(this.analysisData);
      console.log(typeof this.analysisData[0].is_confirm);

      serviceAnalysisSubmit({
        server_analysis_id: this.analysisData[0].id,
        is_confirm: this.analysisData[0].is_confirm ? 1 : 0,
        demand_specification: this.analysisData[0].demand_specification,
        advice_confirm_time: this.analysisData[0].advice_confirm_time,
        expect_acceptance_time: this.analysisData[0].expect_acceptance_time,
        node_id: this.currentNode,
      }).then((res) => {
        if (res.code == 200) {
          console.log(this.result);
          if (this.currentNode == "T10347" || this.currentNode == "T10091") {
            this.$router.push({
              path: "/service/node-flow",
              query: {
                id: this.id,
                showUser: false,
                source: this.currentNode,
                idgroup: this.result,
                backInfo:this.baseData
              },
            });
          } else {
            this.$router.push({
              path: "/service/node-flow",
              query: {
                id: this.id,
                showUser: false,
                source: this.currentNode,
                backInfo:this.baseData
              },
            });
          }
        } else {
          Toast(res.message);
        }
      });
    },
    onSelect(active, index) {
      console.log(active, index);
      if (active.name == "审批记录") {
        this.$router.push({
          path: "/service/approvalRecord",
          query: {
            id: this.id,
          },
        });
      } else if (active.name == "OA流程") {
        this.$router.push({
          path: "/service/oaFlow",
          query: {
            id: this.id,
          },
        });
      }
    },
    saveData() {},
    //获取时间
    getDate(index, dateType) {
      console.log(index, dateType);
      (this.index = index), (this.dateType = dateType);
      this.show = true;
    },
    onConfirm(value) {
      if (this.dateType == 1) {
        this.analysisData[this.index].advice_confirm_time =
          this.formatDate(value);
      } else {
        this.analysisData[this.index].expect_acceptance_time =
          this.formatDate(value);
      }
      // console.log(value);
      this.show = false;
    },
    formatDate(date) {
      let month =
        date.getMonth() + 1 > 9
          ? date.getMonth() + 1
          : "0" + (date.getMonth() + 1);
      let day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
      return `${date.getFullYear()}-${month}-${day}`;
    },
    getAnalysisData() {
      serviceAnalysis({
        server_id: this.id,
        node_id: this.currentNode,
      }).then((res) => {
        console.log(res);
        if (res.code) {
          if (res.data.length > 0) {
            this.analysisData = res.data;
          } else {
            this.analysisData = [
              {
                involve_system_name: "",
                is_confirm: false,
                demand_specification: "",
                advice_confirm_time: "",
                expect_acceptance_time: "",
              },
            ];
          }
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.analysis.padbottom {
  padding-bottom: 100px;
}
.module {
  margin-top: 12px;
}
/deep/.labelColor {
  color: rgba(49, 53, 65, 0.6);
}
/deep/.van-cell__title {
  color: rgba(49, 53, 65, 0.6);
}
/deep/.van-field__control {
  color: #313541;
}
.form-btn {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  background: #fff;
  height: 34px;
  padding: 13px 15px;
  padding-bottom: calc(13px + env(safe-area-inset-bottom));
  .btn {
    flex: 1;
    height: 34px;
    line-height: 34px;
    text-align: center;
    font-size: 14px;
    margin-right: 6px;
    border-radius: 5px;
    &:last-child {
      margin-right: 0;
    }
  }
  .btn-color1 {
    background: rgba(147, 157, 173, 0.18);
    color: #313541;
  }
  .btn-color3 {
    background: #009966;
    color: #fff;
  }
}
</style>